﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MO.BusinessServer
{
    public class SysOperation
    {
        public string Guid()
        {
            return System.Guid.NewGuid().To16String();
        }
        public UserDefine User { get; set; }
        public void Log(object mes)
        {
            MO.Log.WriteRun(mes == null ? "null" : mes.ToString());
        }
        public void Insert(Dictionary<string, object> dict)
        {
            string tableName = dict["__TableName"].ToString();
            dict.Remove("__TableName");
            DbOperation.DB.Insert(tableName, dict);
            dict["__TableName"] = tableName;
        }
        public void Update(Dictionary<string, object> dict)
        {
            string tableName = dict["__TableName"].ToString();
            dict.Remove("__TableName");
            string key = dict[tableName + "_CODE"].ToString();
            dict.Remove(tableName + "_CODE");
            dict.Add("\r", null);
            dict[tableName + "_CODE"] = key;
            DbOperation.DB.Update(tableName, dict);
            dict.Remove("\r");
            dict["__TableName"] = tableName;
        }
        public void SendForm(string userCode, string formCode, string DataCode)
        {
            MessageItem mess = new MessageItem();
            mess.UserCode = userCode;
            mess.Title = "流程处理";
            mess.Type = MessageType.Form;
            mess.Url = string.Format("/FormPage.aspx?FormCode={0}&DataCode={1}", formCode, DataCode);
            var form = DataManager.FindFormByFormCode(formCode);
            mess.Content = string.Format("请处理 {0}", form.Name);
            MessageManager.SendMessage(mess);
        }
        public List<string[]> FindEnum(string entityCode)
        {
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from " + entityCode);
            List<string[]> result = new List<string[]>();
            foreach (DataRow row in table.Rows)
            {
                result.Add(new string[2] { row[entityCode + "_Code"].ToString(), row[entityCode + "_Name"].ToString() });
            }
            return result;
        }
    }
}
